<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
    <style>
        .layui-table td, .layui-table th {
            min-width: 40px;
        }
        .layui-table-cell {
            padding: 0px 10px;
        }
    </style>
</head>

<body class='labelAuto'>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">施工管理</a>
            <a><cite>工人分账管理</cite></a>
        </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right;margin-right:10px"
       onclick="window.history.go(-1)" title="后退">
        <i class="iconfontBlod" style="line-height:30px">&#xe74f;</i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body search">
                    <form action="" class="layui-form">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">月历</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" id="month" placeholder="yyyy-MM">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn search" type="button" id='search'>
                                    <i class="iconfont layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body tableCardBody">
                    <table class="layui-table" id="lists" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type='text/html' id='toolbar'>

</script>
<script src="../statics/plugins/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/javascript">
    layui.use(['layer', "table", "laydate",'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var form = layui.form;
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
        var days = (new Date(year, month, 0)).getDate();
        var curYear = date.getFullYear();
        var curMonth = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);

        var projectId = getParam('projectId');
        //用来记录账目明细中某工人查看其他项目的杂工账目时的项目id

        //日历式杂工账

        var column = [ //表头
            {field: '', title: '编号', type: 'numbers', width: 50, fixed: 'left'},
            {field: 'employeeName', title: '姓名', width: 80, fixed: 'left'},
            {field: 'jobNumber', title: '工号', width: 60, fixed: 'left'},
            // {field: 'projectName', title: '项目', width: 120, fixed: 'left'},
            {field: 'professionName', title: '工种', width: 90, fixed: 'left'},
            {
                field: 'employeeStatus', title: '状态', width: 60, fixed: 'left', templet(d) {
                    return d.employeeStatus == 1 ? "在岗" : d.employeeStatus == 2 ? "待岗" : d.employeeStatus == 3 ? "离职" : d.employeeStatus == 4 ? "请假" : "";
                }
            },
            {field: 1, title: 1, width: 75, templet: function (d) {return columnCallBack(d,1)}},
            {field: 2, title: 2, width: 75, templet: function (d) {return columnCallBack(d,2)}},
            {field: 3, title: 3, width: 75, templet: function (d) {return columnCallBack(d,3)}},
            {field: 4, title: 4, width: 75, templet: function (d) {return columnCallBack(d,4)}},
            {field: 5, title: 5, width: 75, templet: function (d) {return columnCallBack(d,5)}},
            {field: 6, title: 6, width: 75, templet: function (d) {return columnCallBack(d,6)}},
            {field: 7, title: 7, width: 75, templet: function (d) {return columnCallBack(d,7)}},
            {field: 8, title: 8, width: 75, templet: function (d) {return columnCallBack(d,8)}},
            {field: 9, title: 9, width: 75, templet: function (d) {return columnCallBack(d,9)}},
            {field: 10, title: 10, width: 75, templet: function (d) {return columnCallBack(d,10)}},
            {field: 11, title: 11, width: 75, templet: function (d) {return columnCallBack(d,11)}},
            {field: 12, title: 12, width: 75, templet: function (d) {return columnCallBack(d,12)}},
            {field: 13, title: 13, width: 75, templet: function (d) {return columnCallBack(d,13)}},
            {field: 14, title: 14, width: 75, templet: function (d) {return columnCallBack(d,14)}},
            {field: 15, title: 15, width: 75, templet: function (d) {return columnCallBack(d,15)}},
            {field: 16, title: 16, width: 75, templet: function (d) {return columnCallBack(d,16)}},
            {field: 17, title: 17, width: 75, templet: function (d) {return columnCallBack(d,17)}},
            {field: 18, title: 18, width: 75, templet: function (d) {return columnCallBack(d,18)}},
            {field: 19, title: 19, width: 75, templet: function (d) {return columnCallBack(d,19)}},
            {field: 20, title: 20, width: 75, templet: function (d) {return columnCallBack(d,20)}},
            {field: 21, title: 21, width: 75, templet: function (d) {return columnCallBack(d,21)}},
            {field: 22, title: 22, width: 75, templet: function (d) {return columnCallBack(d,22)}},
            {field: 23, title: 23, width: 75, templet: function (d) {return columnCallBack(d,23)}},
            {field: 24, title: 24, width: 75, templet: function (d) {return columnCallBack(d,24)}},
            {field: 25, title: 25, width: 75, templet: function (d) {return columnCallBack(d,25)}},
            {field: 26, title: 26, width: 75, templet: function (d) {return columnCallBack(d,26)}},
            {field: 27, title: 27, width: 75, templet: function (d) {return columnCallBack(d,27)}},
            {field: 28, title: 28, width: 75, templet: function (d) {return columnCallBack(d,28)}},
            {field: 29, title: 29, width: 75, templet: function (d) {return columnCallBack(d,29)}},
            {field: 30, title: 30, width: 75, templet: function (d) {return columnCallBack(d,30)}},
            {field: 31, title: 31, width: 75, templet: function (d) {return columnCallBack(d,31)}},
            {
                field: 'balance', title: '结余', width: 80, templet: function (d) {
                    var balance = 0;
                    for (var i in d.accounts) {
                        balance += Number(d.accounts[i]);
                    }
                    return balance;
                }
            },
        ];

        var minus = 31 - days;
        var calendar_column = column.slice(0);
        for (var m = 0; m < minus; m++) {
            calendar_column.splice(-2,1);
        }

        var calendarOption = {
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '杂工账目表',
            height: 'full-150',
            contentType: "application/json; charset=utf-8",
            url: url + '/project/project/workmanAccountsList', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            method: 'POST',
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count": res.data.count
                };
            },
            where: {
                projectId: getParam('projectId'),
                month: '',
                token: getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: [
                'filter',
                {title: '导出', layEvent: 'excel', icon: 'layui-icon-export'}
            ],
            page: false, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [calendar_column],
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            size: 'sm',
            done: function (res, curr, count) {
                this.cols = []; //（关键代码）将cols初始化，否则表格重载时无法正确重新渲染表头
                permission();
            }
        };

        laydate.render({
            elem: '#month'
            , type: 'month'
            , value: curYear + '-' + curMonth
            , max: 0
        });
        var lists = table.render(calendarOption);

        table.on('tool(test)', function (data) {

        });
        table.on('toolbar(test)', function (data) {
            if (data.event == 'excel') {
                var exportProjectId = '';
                var searchStr = '';
                if($('.thisProject').length>0){
                    exportProjectId = $('.thisProject').data('id');
                }else{
                    exportProjectId = projectId
                }

                var startTime ='';
                var endTime ='';
                var month_search = $('input#month').val();
                var exportYear = month_search.substr(0, 4);
                var exportMonth = month_search.substr(5, 2);
                var exportDays = (new Date(exportYear, exportMonth, 0)).getDate();
                searchStr = '';
                startTime = month_search+'-01';
                endTime = month_search+'-'+exportDays;

                layer.open({
                    type: 0,
                    title: "杂工分账",
                    area: ["40%", '45%'],
                    content: `
                            <form class="layui-form layerOpen" action="">
                                <div class="layui-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">项目</label>
                                        <div class="layui-input-inline">
                                            ${$('button.thisProject').text()?$('button.thisProject').text():'当前项目'}
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">时间</label>
                                        <div class="layui-input-inline">
                                            ${(startTime=='' && endTime=='')?'不限时间':startTime+'至'+endTime}
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">搜索条件</label>
                                        <div class="layui-input-inline">
                                            ${searchStr===''?'无':searchStr}
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-item">
                                    <div class="layui-inline">
                                        <div class="layui-input-inline">
                                            即将导出符合以上条件的杂工账目，点击“确认”导出
                                        </div>
                                    </div>
                                </div>
                            </form>
                        `,
                    btn: ['确定', '关闭'],
                    skin: 'my-skin',
                    success: function () {

                    },
                    yes: function () {
                        if (startTime !== '' && endTime !== '') {
                            var s = new Date(startTime);
                            var e = new Date(endTime);
                            if (e < s) {
                                layer.open({
                                    type: 0,
                                    title: "杂工分账",
                                    area: ["40%", '30%'],
                                    content: `请选择截止时间不能小于起始时间`,
                                    btn: ['关闭'],
                                    skin: 'my-skin'
                                })
                                return
                            }
                        }

                        layer.closeAll();
                        layer.open({
                            type: 0,
                            title: "杂工分账",
                            area: ["40%", '30%'],
                            content: `当前杂工分账记录正在生成中,请稍等!`,
                            btn: ['关闭'],
                            skin: 'my-skin'
                        });
                        $.ajax({
                            url: url + '/excel/export/handymanAccounts',
                            type: 'post',
                            data: {
                                projectId: exportProjectId,
                                startTime: startTime,
                                endTime: endTime,
                                search:searchStr,
                                token: getParam('token')
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    layer.closeAll();
                                    layer.open({
                                        type: 0,
                                        title: "导出",
                                        area: ["40%", '30%'],
                                        content: `<p class='one_p'>${r.msg}</p>`,
                                        btn: ['关闭'],
                                        skin: 'my-skin'
                                    })
                                } else {
                                    layer.closeAll();
                                    layer.open({
                                        type: 0,
                                        title: "导出",
                                        area: ["40%", '30%'],
                                        content: `<p class='one_p'>${r.msg}</p>`,
                                        btn: ['关闭'],
                                        skin: 'my-skin'
                                    })
                                }
                            }
                        })
                    }
                });
            }
        });

        //搜索
        $('body').on('click', '#search', function () {
            var month_search = $('input#month').val();
            year = month_search.substr(0, 4);
            month = month_search.substr(5, 2);
            days = (new Date(year, month, 0)).getDate();
            minus = 31 - days;
            tmpColumn = column.slice(0);
            for (var m = 0; m < minus; m++) {
                tmpColumn.splice(-2,1);
            }
            table.reload('lists', {
                cols: [
                    tmpColumn
                ],
                where: {
                    token: getToken(),
                    projectId: projectId,
                    month: month_search
                }
            })
        });

        function columnCallBack(d,i){
            return d.accounts[i] !== '' ? Number(d.accounts[i]) : '';
        }
    });
</script>

</html>